<?php
class ConfigModel extends ModelBase
{
	protected static $_tableName = 'config';
	
	public function add($name, $label, $value, $type='text')
	{
		$value = htmlspecialchars($value);
		$data = array(
			'name'	=> $name,
			'value'	=> $value,
			'label'	=> $label,
			'type'	=> $type
		);
		return self::queryInsert(self::$_tableName, $data);
	}
	
	public function update($name, $data)
	{
		$where = "`name`='{$name}'";
		if (isset($data['value'])) $data['value'] = htmlspecialchars($data['value']);
		return self::queryUpdate(self::$_tableName, $data, $where);
	}
	
	public function get($id='', $name='')
	{
		if (!$id && !$name){
			return false;
		}
		if ($id){
			$where = "id={$id}";
		}else{
			$where = "name='{$name}'";
		}
		$data = self::querySelectSingle(self::$_tableName, array('*'), $where);
		if ($data) {
			$data['value'] = htmlspecialchars_decode($data['value']);
		}
		return $data;
	}
	
	public function getList()
	{
		$data = self::querySelect(self::$_tableName, array('*'));
		if ($data) {
			foreach ($data as &$item){
				$item['value'] = htmlspecialchars_decode($item['value']);
			}
		}
		return $data;
	}
	
	public function delete($id='', $name='')
	{
		if (!$id && !$name){
			return false;
		}
		if ($id){
			$where = "id={$id}";
		}else{
			$where = "name='{$name}'";
		}
		return self::queryDelete(self::$_tableName, $where);
	}
}